#ifdef WIN32
#include <windows.h>
#endif

#include "tankfireengine.h"
#include "tankmissile.h"
#include <GL/gl.h>

using namespace bend2d;

TankFireEngine::TankFireEngine(const Point& position) : 
     Box(bend2d::Rectangle(position, Angle(0), 10, 10))
{
}

void TankFireEngine::Draw()
{
     glPushMatrix();
     glLineWidth(3.0);
     
     glColor3d(m_EngineColor.GetRedFloat(), m_EngineColor.GetGreenFloat(), m_EngineColor.GetBlueFloat());

     const bend2d::Rectangle& rectangle = GetRectangle();
     glTranslated(rectangle.GetCenter().GetX(), rectangle.GetCenter().GetY(), 0);
     glRotated(GetAngle().GetAngle(), 0, 0, 1);
     
     glBegin(GL_POLYGON);
         glVertex2d(rectangle.GetAbsLeft(), rectangle.GetAbsTop());
         glVertex2d(rectangle.GetAbsRight(), rectangle.GetAbsTop());
         glVertex2d(rectangle.GetAbsRight(), rectangle.GetAbsBottom());
         glVertex2d(rectangle.GetAbsLeft(), rectangle.GetAbsBottom());
     glEnd();

     Point cannon(25, Angle(0));
     glBegin(GL_LINES);
        glVertex2d(0, 0);
        glVertex2d(cannon.GetX(), cannon.GetY());
     glEnd();

     glPopMatrix();
}

void TankFireEngine::Shot(World* world)
{
    Point cannon(25, GetAngle()); 
    TankMissile* missile = new TankMissile(GetPosition() + cannon, this);

    missile->SetAcceleration(Acceleration(100, GetAngle()));
    missile->SetMaxSpeed(Speed(0, Angle(0)));
    world->RegistryObject(missile);
}
